package top.zywork.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import top.zywork.annotation.SysLog;
import top.zywork.common.BeanUtils;
import top.zywork.common.BindingResultUtils;
import top.zywork.constant.CommonConstant;
import top.zywork.dos.SchoolDO;
import top.zywork.dos.SchoolPlaceDO;
import top.zywork.dto.PagerDTO;
import top.zywork.dto.SchoolParcelDTO;
import top.zywork.dto.SchoolPlaceDTO;
import top.zywork.query.SchoolPlaceCbtQuery;
import top.zywork.query.SchoolPlaceQuery;
import top.zywork.service.SchoolPlaceService;
import top.zywork.service.SchoolService;
import top.zywork.vo.*;
import top.zywork.vo.wx.SchoolPlaceVO;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  取件点前端控制器
 * </p>
 *
 * @author
 * @since 2020-10-13
 */
@RestController
@RequestMapping("/school-place")
@Slf4j
public class SchoolPlaceController {

    @Autowired
    private SchoolPlaceService schoolPlaceService;

    @Autowired
    private SchoolService schoolService;

    /**
     * 通过学校ID查询对应的取件点
     * @param schoolId 学校ID
     * @return
     */
    @PostMapping("any/bySchoolId/{id}")
    public ResponseStatusVO bySchoolId(@PathVariable("id") Long schoolId){
        List list = schoolPlaceService.list(new QueryWrapper<SchoolPlaceDO>()
                .eq("is_active", CommonConstant.IS_ACTIVE_TRUE)
                .eq("school_id", schoolId));
        return ResponseStatusVO.ok("查询成功",BeanUtils.copyList(list, SchoolPlaceVO.class));
    }

    @PostMapping("admin/save")
    @SysLog(description = "后台保存SchoolPlaceVO")
    public ResponseStatusVO save(@RequestBody @Validated SchoolPlaceDTO schoolPlaceDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        SchoolPlaceDO copy = BeanUtils.copy(schoolPlaceDTO, SchoolPlaceDO.class);
        copy.setIsActive(CommonConstant.IS_ACTIVE_TRUE);
        copy.setCreateTime(new Date());
        copy.setUpdateTime(new Date());
        schoolPlaceService.save(copy);
        return ResponseStatusVO.ok("添加成功", null);
    }

    @PostMapping("admin/batch-save")
    @SysLog(description = "后台批量保存SchoolPlaceVO")
    public ResponseStatusVO saveBatch(@RequestBody @Validated List<SchoolPlaceVO> schoolPlaceVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        BeanUtils.copyListObj(schoolPlaceVOList, SchoolPlaceDO.class);

        List<SchoolPlaceDO> list;
        list = schoolPlaceVOList.stream().map(e->BeanUtils.copy(e,SchoolPlaceDO.class)).collect(Collectors.toList());

        schoolPlaceService.saveBatch(list);
        return ResponseStatusVO.ok("批量添加成功", null);
    }

    @GetMapping("admin/remove/{id}")
    @SysLog(description = "后台删除SchoolPlaceVO")
    public ResponseStatusVO removeById(@PathVariable("id") Long id) {
        schoolPlaceService.removeById(id);
        return ResponseStatusVO.ok("删除成功", null);
    }

    @PostMapping("admin/batch-remove")
    @SysLog(description = "后台批量删除SchoolPlaceVO")
    public ResponseStatusVO removeByIds(@RequestBody String[] ids) {
        List<String> id = new ArrayList<>();
        Collections.addAll(id, ids);
        schoolPlaceService.removeByIds(id);
        return ResponseStatusVO.ok("批量删除成功", null);
    }

    @PostMapping("admin/update")
    @SysLog(description = "后台更新SchoolPlaceVO")
    public ResponseStatusVO update(@RequestBody @Validated SchoolPlaceDTO schoolPlaceDTO, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        SchoolPlaceDO copy = BeanUtils.copy(schoolPlaceDTO, SchoolPlaceDO.class);
        copy.setUpdateTime(new Date());
        boolean result = schoolPlaceService.updateById(copy);
        if (result) {
            return ResponseStatusVO.ok("更新成功", null);
        } else {
            return ResponseStatusVO.dataError("数据版本号有问题，在此更新前数据已被更新", null);
        }
    }

    @PostMapping("admin/batch-update")
    @SysLog(description = "后台批量更新SchoolPlaceVO")
    public ResponseStatusVO updateBatch(@RequestBody @Validated List<SchoolPlaceVO> schoolPlaceVOList, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return ResponseStatusVO.dataError(BindingResultUtils.errorString(bindingResult), null);
        }
        List<SchoolPlaceDO> list;
        list = schoolPlaceVOList.stream().map(e->BeanUtils.copy(e,SchoolPlaceDO.class)).collect(Collectors.toList());
        schoolPlaceService.updateBatchById(list);
        return ResponseStatusVO.ok("批量更新成功", null);
    }

    @PostMapping("admin/active")
    @SysLog(description = "后台激活或冻结SchoolPlaceVO")
    public ResponseStatusVO active(@RequestBody SchoolPlaceDTO schoolPlaceDTO) {
        schoolPlaceService.update(new UpdateWrapper<SchoolPlaceDO>().eq("id",schoolPlaceDTO.getId())
                .set("is_active",schoolPlaceDTO.getIsActive()));
        return ResponseStatusVO.ok("激活或冻结成功", null);
    }

    @PostMapping("admin/batch-active")
    @SysLog(description = "后台批量激活或冻结SchoolPlaceVO")
    public ResponseStatusVO activeBatch(@RequestBody @Validated List<SchoolPlaceDTO> schoolPlaceDTOS) {
        List<SchoolPlaceDO> list;
        list = schoolPlaceDTOS.stream().map(e->BeanUtils.copy(e,SchoolPlaceDO.class)).collect(Collectors.toList());
        schoolPlaceService.updateBatchById(list);
        return ResponseStatusVO.ok("批量激活或冻结成功", null);
    }

    @GetMapping("admin/one/{id}")
    public ResponseStatusVO getById(@PathVariable("id") Long id) {
        SchoolPlaceVO schoolPlaceVO = new SchoolPlaceVO();
        Object obj = schoolPlaceService.getById(id);
        if (obj != null) {
            schoolPlaceVO = BeanUtils.copy(obj, SchoolPlaceVO.class);
        }
        return ResponseStatusVO.ok("查询成功", schoolPlaceVO);
    }

    @GetMapping("admin/all")
    public ResponseStatusVO listAll() {
        IPage page = schoolPlaceService.page(null);
        PagerDTO pagerDTO = PagerDTO.builder().total(page.getTotal()).rows(page.getRecords()).build();
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SchoolPlaceDTO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/all-cond")
    public ResponseStatusVO listAllByCondition(@RequestBody SchoolPlaceQuery schoolPlaceQuery) {
        PagerDTO pagerDTO = schoolPlaceService.listAllByCondition(schoolPlaceQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);
        pagerVO.setRows(BeanUtils.copyList(pagerDTO.getRows(), SchoolPlaceDTO.class));
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

    @PostMapping("admin/pager-cond")
    public ResponseStatusVO listPageByCondition(@RequestBody SchoolPlaceQuery schoolPlaceQuery) {
        PagerDTO pagerDTO = schoolPlaceService.listPageByCondition(schoolPlaceQuery);
        PagerVO pagerVO = BeanUtils.copy(pagerDTO, PagerVO.class);

        List<Object> list;

        list = pagerDTO.getRows().stream().map(e->{
            SchoolPlaceDO schoolPlaceDO = (SchoolPlaceDO)e;
            SchoolPlaceDTO schoolPlaceDTO = new SchoolPlaceDTO();
            BeanUtils.copy(schoolPlaceDO,schoolPlaceDTO);
            schoolPlaceDTO.setSchoolName(schoolService.getById(schoolPlaceDO.getSchoolId()).getSchool());
            return schoolPlaceDTO;
        }).collect(Collectors.toList());
        pagerVO.setRows(list);
        return ResponseStatusVO.ok("查询成功", pagerVO);
    }

}
